//
//  ChangePhoneViewController2.m
//  suRead
//
//  Created by 师爱华 on 16/6/25.
//  Copyright © 2016年 bao. All rights reserved.
//

#import "ChangePhoneViewController2.h"
#import "ServiceUser.h"
#import "UIViewController+HUD.h"
#import "AccountManager.h"


#import "AppDelegate.h"
#import "UIApplication+VisibleViewController.h"
#import "LoginViewController.h"
#import "HomeTabBarController.h"

@interface ChangePhoneViewController2 ()

@property (strong, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UIButton *getCodeButton;
@end

@implementation ChangePhoneViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
//    CGRect r = [ UIScreen mainScreen ].applicationFrame;
//    //    [self mainView].center = self.view.center;
//    [self mainView].center = CGPointMake(r.size.width/2, [self mainView].center.y);

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated {
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    //    [self mainView].center = self.view.center;
    [self mainView].center = CGPointMake(r.size.width/2, [self mainView].center.y);
    
    
}



- (void)setTitle:(NSString *)title
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTap:)];
    [self.mainView addGestureRecognizer:tap];
    self.mainView.userInteractionEnabled = YES;
    [self.getCodeButton addTarget:self action:@selector(getCode:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)getCode:(id)sender
{
    __weak typeof(self) weak_self = self;
    
    self.getCodeButton.userInteractionEnabled=NO;
    self.getCodeButton.alpha=0.4;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(60.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        weak_self.getCodeButton.userInteractionEnabled=YES;
        weak_self.getCodeButton.alpha=1.0f;
    });
    [[[[UIApplication sharedApplication] keyWindow] rootViewController] showHUDProgress:nil];
    [ServiceUser doGetPhoneCode:self.phoneText.text
                        success:^(id responseObject, NSString *message) {
                            [[[[UIApplication sharedApplication] keyWindow] rootViewController] removeHUDProgress];
                            [[[[UIApplication sharedApplication] keyWindow] rootViewController] showHUDText:message];
                        } failure:^(NSString *message) {
                            [[[[UIApplication sharedApplication] keyWindow] rootViewController] removeHUDProgress];
                            [[[[UIApplication sharedApplication] keyWindow] rootViewController] showHUDText:message];
                            weak_self.getCodeButton.userInteractionEnabled=YES;
                            weak_self.getCodeButton.alpha=1.0f;
                        }];
}
- (void)viewTap:(id)sender
{
        if (self.baViewTapCallBackBlock) {
            self.baViewTapCallBackBlock();
        }
}

- (IBAction)shortLoginout:(id)sender
{
    [AccountManager sharedAccountManager].f_isLogined = NO;
    [AccountManager sharedAccountManager].f_nickname = @"";
    [AccountManager sharedAccountManager].f_email = @"";
    [AccountManager sharedAccountManager].f_uid = @"";
    [AccountManager sharedAccountManager].f_userName = @"";
    if (self.callBackBlock) {
        [self dismissViewControllerAnimated:YES completion:nil];
        self.callBackBlock();
    }
    
}

- (IBAction)submit:(id)sender {
    __weak typeof(self) weak_self = self;
    if(self.pwdText.text.length==0) {
        [[[[UIApplication sharedApplication] keyWindow] rootViewController] showHUDText:@"请输入密码"];
        return;
    }
    
    [ServiceUser doThirdPartBindingPhone:[AccountManager sharedAccountManager].f_uid
                                phoneNum:self.phoneText.text
                                    code:self.codeText.text
                                password:self.pwdText.text
                                 success:^(id responseObject, NSString *message) {
                                     [[[[UIApplication sharedApplication] keyWindow] rootViewController] showHUDText:message];
                                     if (weak_self.successBlock) {
                                         weak_self.successBlock();
                                     }
                                     self.phoneText.text = @"";
                                     self.codeText.text = @"";
                                     self.pwdText.text=@"";
                                     weak_self.getCodeButton.userInteractionEnabled=YES;
                                     weak_self.getCodeButton.alpha=1.0f;
                                 } failure:^(NSString *message) {
                                     [[[[UIApplication sharedApplication] keyWindow] rootViewController] showHUDText:message];
                                 }];
    
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
